home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cool / ge_cool.lha / GE_COOL2.1 / src / String / String.h < prev   
C/C++ Source or Header  |  1992-05-18  |  19KB  |  507 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Created: MBN 06/27/89 -- Initial design and implementation
  13. // Updated: DKM 07/07/89 -- To work around Xenix 31 char limit:
  14. //                          Shortened is_less_than        to is_lt
  15. //                                    is_greater_than     to is_gt
  16. //                                    is_less_or_equal    to is_le
  17. //                                    is_greater_or_equal to is_ge
  18. //                          Removed is_equal_or_less and is_greater_or_less
  19. // Updated: MBN 08/03/89 -- Changed operator= argument to const
  20. // Updated: LGO 08/09/89 -- Inherit from Generic
  21. // Updated: MBN 09/06/89 -- Added conditional exception handling
  22. // Updated: LGO 10/28/89 -- Removed is_lt, is_gt, is_le, is_ge, is_equal
  23. //                          and is_not_equal (use char* functions instead)
  24. // Updated: LGO 11/07/89 -- Removed strcmp, strncmp
  25. // Updated: MBN 12/15/89 -- Sprinkled "const" all over the place!
  26. // Updated: LGO 01/05/90 -- Removed strchr, strrchr
  27. // Updated: MBN 01/19/90 -- Made operator=(const char*) take a const char*
  28. // Updated: MJF 06/15/90 -- Added inline strncpy(...,int) to remove ambiguity
  29. // Updated: DLS 03/22/91 -- New lite version
  30. //
  31. // The String class provides dynamic, efficient  strings for a C++ application
  32. // programmer.  The string private data consists of a  slot that maintains the
  33. // length  of   the string  (ie.   number of characters),   a  size  slot that
  34. // maintains the  actual number of  bytes allocated to  a string  object char*
  35. // pointer, and a pointer to the first character of the string. In addition, a
  36. // floating point slot can contain a non-negative percentage between 0 and 1.0
  37. // that  indicates   the ratio by which  a   string object  should  grow  when
  38. // necessary.  Finally,  a static for  the  entire  string class  contains the
  39. // allocation size to be used  when a string  object needs to grow dynamically
  40. // if the growth ratio for the particular instance has not been set.  This has
  41. // a default value that may be over-ridden by the user when the constructor is
  42. // invoked.
  43. //
  44. //              String               Virtual Function Table
  45. //           +-----------+                +----------+
  46. //           |  V_Table--+------------->  :Alloc_Size:
  47. //           +-----------+                :  ....    :
  48. //           | Length=16 |                :          :
  49. //           +-----------+                +----------+
  50. //           | size=100  |                
  51. //           +-----------+   
  52. //           | ratio=0.0 |
  53. //           +-----------+       +--------------------- ... --+
  54. //           |   str   --+------>|This is a string            |
  55. //           +-----------+       +--------------------- ... --+
  56. //
  57. //                               \_____________  _____________/
  58. //                                             \/
  59. //                                     100 bytes allocated
  60. //
  61. // There are  several constructors  for  this class.  The   empty  constructor
  62. // initializes a String object and allocates the default size block of memory.
  63. // The second, third,  and fourth constructors  take char, char*,   and String
  64. // arguments, respectively, and initialize the  String object accordingly. The
  65. // fifth and sixth constructors take either a char* or String argument, and an
  66. // integer argument that specifies the initial
  67. //
  68. // The standard ANSI "str____" function names are all overloaded  for use with
  69. // both  char* and   String  objects.   Operators   for  String concatenation,
  70. // assignment, and comparison are also provided.   In  addition, functions for
  71. // case conversion and string token trimming are provided.  Finally, note that
  72. // the operator functions use corresponding functions with the "case" flag set
  73. // to SENSITIVE.  A user can perform case-INSENSITIVE operations by explicitly
  74. // calling the appropriate function.
  75. //
  76.  
  77. #ifndef STRINGH                    // If no String class defined
  78. #define STRINGH                    // Indicate its done now
  79.  
  80. #ifndef CHARH                    // If extended char* not here
  81. #include <cool/char.h>
  82. #endif
  83.  
  84. #if defined(DOS)
  85. extern "C" {
  86. #include <stdlib.h>        // Include standard c library support
  87. }
  88. #else
  89. #include <stdlib.h>        // Include standard c library support
  90. #endif
  91.  
  92. #define MEM_BLK_SZ 100
  93.  
  94. class CoolStringE;                // forward dec. of envelope
  95.  
  96. class CoolString {  
  97. public:
  98.   CoolString ();                // CoolString x;
  99.   CoolString (char);                // CoolString x = 'A';
  100.   CoolString (const char*);            // CoolString x = "ABCDEFG";
  101.   CoolString (const CoolString&);        // CoolString x = y;
  102.   CoolString (const CoolString&, long);        // CoolString x = y; memory size
  103.   CoolString (const char*, long);        // CoolString x = "ABCDEFG"; size
  104.  
  105.   ~CoolString();                // Destructor for CoolString class
  106.  
  107.   Boolean insert (const char*, long);        // Insert chars at index
  108.   Boolean remove (long, long);            // Remove chars between indexes
  109.   Boolean replace (const char*, long, long);    // Replace chars between index
  110.   void yank (CoolString&, long, long);        // Delete/set to chars at index
  111.   void sub_string (CoolString&, long, long);    // Set to chars between indexes
  112.  
  113.   friend CoolString& strncpy (CoolString&, const char*, long); // Copy "n" chars
  114.   inline friend CoolString& strncpy (CoolString&, const char*, int);
  115.  
  116.   friend ostream& operator<< (ostream&, const CoolString&);
  117.   friend ostream& operator<< (ostream&, const CoolString*);
  118.   
  119.   inline CoolString& operator= (char);        // x = 'A';
  120.   inline CoolString& operator= (const char*);    // x = "ABCDEFG";
  121.   inline CoolString& operator= (const CoolString&);    // x = y;
  122.   inline CoolString& operator= (CoolStringE&);    // from envelope back to string
  123.  
  124.   inline CoolStringE operator+ (char) const;    // Concatenation operators
  125.   inline CoolStringE operator+ (const char*) const;    
  126.  
  127. // Avoid deep copy and concatenate strings in place with envelope
  128. //   inline friend CoolString operator+ (const CoolString&, const CoolString&);
  129.   
  130.   inline CoolString& operator+= (char);        // Concatentation w/ assignment
  131.   inline CoolString& operator+= (const char*);
  132.   inline CoolString& operator+= (const CoolString&);
  133.   
  134.   inline operator const char*() const;        // String to const char*
  135.   void reverse ();                // Reverse character order
  136.   void clear ();                // Reset NULL terminator
  137.   void resize (long);                // Allocate at least min size
  138.   inline char& operator[] (long i);        // Specific char from CoolString
  139.  
  140.   inline long capacity() const;            // Returns maximum size string 
  141.   inline void set_alloc_size (int);        // Set memory block alloc size
  142.   inline void set_growth_ratio (float);        // Set growth percentage
  143.   
  144.   inline Boolean operator== (const CoolString&) const; // Equality operator
  145.   inline Boolean operator== (const char*) const;
  146.   
  147.   inline Boolean operator!= (const CoolString&) const; // Inequality operator
  148.   inline Boolean operator!= (const char*) const;
  149.   
  150.   inline Boolean operator< (const CoolString&) const; // Lexical less than
  151.   inline Boolean operator< (const char*) const;
  152.   
  153.   inline Boolean operator> (const CoolString&) const; // Lexical greater than
  154.   inline Boolean operator> (const char*) const;
  155.   
  156.   inline Boolean operator<= (const CoolString&) const; // Lexical less than/equal
  157.   inline Boolean operator<= (const char*) const;
  158.   
  159.   inline Boolean operator>= (const CoolString&) const; // Lexical greater than/eq
  160.   inline Boolean operator>= (const char*) const;
  161.   
  162.   inline friend long strlen (const CoolString&);    // Return length of string
  163.   friend CoolString& strcat(CoolString&, const CoolString&); // Appends a copy of second
  164.   friend CoolString& strcat (CoolString&, const char*);
  165.   friend CoolString& strcat (CoolString&, char);
  166.   
  167.   friend CoolString& strncat (CoolString&, const CoolString&, int); // Append "n" chars
  168.   friend CoolString& strncat (CoolString&, const char*, int);
  169.   
  170.   friend CoolString& strcpy (CoolString&, char);    // CoolString copy functions
  171.   friend CoolString& strcpy (CoolString&, const char*);
  172.   friend CoolString& strcpy (CoolString&, const CoolString&); 
  173.   
  174.   friend long strtol(const CoolString&, char** ptr=NULL, int radix=10); // to long
  175.   friend long atol (const CoolString&);        // Convert string to long
  176.   friend int atoi (const CoolString&);        // Convert string to int
  177.   
  178.   friend double strtod (const CoolString&, char** ptr=NULL); // string to double
  179.   inline friend double atof (const CoolString&);    // Convert string to double
  180.   
  181.   friend CoolString& trim (CoolString&, const char*);        // Trim characters 
  182.   friend CoolString& left_trim (CoolString&, const char*);  // Trim prefix chars
  183.   friend CoolString& right_trim (CoolString&, const char*); // Trim suffix chars
  184.   
  185.   friend CoolString& upcase (CoolString&);        // Convert CoolString to upper 
  186.   friend CoolString& downcase (CoolString&);        // Convert string to lower
  187.   friend CoolString& capitalize (CoolString&);        // Capitalize each word
  188.  
  189. private:
  190.   long length;                    // Number of characters 
  191.   long size;                    // Allocated memory size
  192.   char* str;                    // Pointer to string
  193.   float growth_ratio;                // If non-zero, grow by %
  194.   static int alloc_size_s;            // Memory growth size
  195.  
  196.   void bracket_error (long);            // Raise exception
  197.   void growth_error (int);            // Raise exception
  198.   void ratio_error (float);            // Raise exception
  199.   friend void update_memory (CoolString&);        // Adjust memory size
  200. };
  201.  
  202.  
  203.  
  204. // Avoid deep copy, and concatenate strings in place with envelope
  205.  
  206. #define ENVELOPE_PLUS                // += can be done in place
  207.  
  208. #define CoolLetter CoolString
  209. #define CoolEnvelope CoolStringE        
  210.  
  211. #include <cool/Envelope.h>            // Include envelope macros
  212.  
  213. #undef CoolLetter
  214. #undef CoolEnvelope
  215.  
  216. #undef ENVELOPE_PLUS
  217.  
  218.  
  219. // operator[] -- Return a single character element from CoolString
  220. // Input:        this* CoolString pointer, index "i"
  221. // Output:       The "ith-1" character from CoolString
  222.  
  223. inline char& CoolString::operator[] (long i) {
  224. #if ERROR_CHECKING
  225.   if (i > this->length)                // If index out of range
  226.     this->bracket_error (i);            // Raise exception
  227. #endif
  228.   return (this->str[i]);
  229. }
  230.  
  231.  
  232. // capacity -- Determine the maximum size string possible with growing
  233. // Input:      None
  234. // Output:     Maximum number of characters before growth is required
  235.  
  236. inline long CoolString::capacity() const {
  237.   return (this->size-1);            // Allocated size -1 for NULL
  238. }
  239.  
  240.  
  241. // set_alloc_size -- Set the default allocation size growth rate
  242. // Input:            Growth size in number of elements
  243. // Output:           None
  244.  
  245. inline void CoolString::set_alloc_size (int n) {
  246. #if ERROR_CHECKING
  247.   if (n < 0)                    // If negative growth size
  248.     this->growth_error (n);            // Raise exception
  249. #endif
  250.   this->alloc_size_s = n;            // Set growth size
  251. }
  252.  
  253.  
  254. // set_growth_ratio -- Set the growth percentage for this instance of String
  255. // Input:              Percentage growth rat
  256. // Output:             None
  257.  
  258. inline void CoolString::set_growth_ratio (float ratio) {
  259. #if ERROR_CHECKING
  260.   if (ratio <= 0.0)                // If negative growth factor
  261.     this->ratio_error (ratio);            // Raise exception
  262. #endif
  263.   this->growth_ratio = ratio;            // Set growth size
  264. }
  265.  
  266.  
  267. // operator const char* -- Provide an accessor to the String character pointer
  268. // Input:            this* CoolString pointer
  269. // Output:           this->str character pointer
  270.  
  271. inline CoolString::operator const char*() const {return this->str;}
  272.  
  273. // operator= -- CoolString assignment to a single character: x = 'A';
  274. // Input:       Single character
  275. // Output:      CoolString object containing character string
  276.  
  277. inline CoolString& CoolString::operator= (char c) {
  278.   return (strcpy (*this, c));            
  279. }
  280.  
  281.  
  282. // operator= -- CoolString assignment to a character string: x = "ABCDEFG";
  283. // Input:       Character string
  284. // Output:      CoolString object containing character string
  285.  
  286. inline CoolString& CoolString::operator= (const char* c) {
  287.   return (strcpy (*this, c));            
  288. }
  289.  
  290.  
  291. // operator= -- CoolString assignment to another CoolString: x = y;
  292. // Input:       Reference to CoolString object
  293. // Output:      CoolString object sharing memory with other CoolString
  294.  
  295. inline CoolString& CoolString::operator= (const CoolString& s) {
  296.   return (strcpy (*this, s));            
  297. }
  298.  
  299. // operator=  -- Assignment from an envelope back to real string
  300. // Input:     envelope reference
  301. // Output:    string reference with contents in envelope being swapped over
  302.  
  303. inline CoolString& CoolString::operator= (CoolStringE& env){
  304.   env.shallow_swap((CoolStringE*)this, &env);    // same physical layout
  305.   return *this;
  306. }
  307.  
  308.  
  309. // operator+= -- CoolString concatenation of a character: x += 'A';
  310. // Input:        Character
  311. // Output:       CoolString object concatenated with character
  312.  
  313. inline CoolString& CoolString::operator+= (char c) {
  314.   return (strcat (*this, c));        
  315. }
  316.  
  317. // operator+ -- CoolString concatenation of a character: x = x + 'A';
  318. // Input:       Character
  319. // Output:      new CoolString object concatenated with character
  320.  
  321. inline CoolStringE CoolString::operator+ (char c) const {
  322.   CoolString temp(*this);            // Temporary string
  323.   temp += c;                    // Concatenate temp with c
  324.   CoolStringE& result = (CoolStringE&) temp;    // same physical object
  325.   return result;                // copy of envelope
  326. }
  327.  
  328. // operator+= -- CoolString concatenation of a character string: x += "ABCDEFG";
  329. // Input:        Character CoolString
  330. // Output:       CoolString object concatenated with character CoolString
  331.  
  332. inline CoolString& CoolString::operator+= (const char* c) {
  333.   return (strcat (*this, c));        
  334. }
  335.  
  336.  
  337. // operator+ -- CoolString concatenation of a character string: x = x + "ABCDEFG";
  338. // Input:       Character string
  339. // Output:      CoolString object concatenated with character string
  340.  
  341. inline CoolStringE CoolString::operator+ (const char* c) const {
  342.   CoolString temp(*this);            // Temporary string
  343.   temp += c;                    // Concatenate temp with c
  344.   CoolStringE& result = (CoolStringE&) temp;    // same physical object
  345.   return result;                // copy of envelope
  346. }
  347.  
  348. // operator+= -- CoolString concatenation of another string: x += y;
  349. // Input:        CoolString reference
  350. // Output:       CoolString object concatenated with CoolString contents
  351.  
  352. inline CoolString& CoolString::operator+= (const CoolString& s) {
  353.   return (strcat (*this, s));        
  354. }
  355.  
  356.  
  357. // operator+ -- CoolString concatenation of another string: x = x + y;
  358. //              Implemented as a friend function in CoolEnvelope
  359. // Input:       CoolString reference
  360. // Output:      CoolString object concatenated with CoolString contents
  361.  
  362. // CoolString CoolString::operator+ (const CoolString& s) const {
  363. //   CoolString temp(*this);            // Temporary string
  364. //   strcat(temp, s);                // Concatenate temp with s
  365. //   return temp;                // Return by value
  366. // }
  367.  
  368.  
  369. // operator== -- Test for equality of two CoolString objects
  370. // Input:        this* CoolString pointer, CoolString reference
  371. // Output:       Boolean TRUE/FALSE
  372.  
  373. inline Boolean CoolString::operator== (const CoolString& s) const {
  374.   return (is_equal (*this, s.str, SENSITIVE));
  375. }
  376.  
  377.  
  378. // operator== -- Test for equality of a CoolString and char*
  379. // Input:        this* CoolString pointer, char* pointer
  380. // Output:       Boolean TRUE/FALSE
  381.  
  382. inline Boolean CoolString::operator== (const char* c) const {
  383.   return (is_equal (*this, c, SENSITIVE));
  384. }
  385.  
  386.  
  387. // operator!= -- Test for inequality of two CoolString objects
  388. // Input:        this* CoolString pointer, CoolString reference
  389. // Output:       Boolean TRUE/FALSE
  390.  
  391. inline Boolean CoolString::operator!= (const CoolString& s) const {
  392.   return (is_not_equal (*this, s.str, SENSITIVE));
  393. }
  394.  
  395.  
  396. // operator!= -- Test for inequality of two CoolString objects
  397. // Input:        this* CoolString pointer, char* pointer
  398. // Output:       Boolean TRUE/FALSE
  399.  
  400. inline Boolean CoolString::operator!= (const char* c) const {
  401.   return (is_not_equal (*this, c, SENSITIVE));
  402. }
  403.  
  404.  
  405. // operator< -- Test for lexical ordering before a CoolString
  406. // Input:       this* CoolString pointer, CoolString reference
  407. // Output:      Boolean TRUE/FALSE
  408.  
  409. inline Boolean CoolString::operator< (const CoolString& s) const {
  410.   return (is_lt (*this, s.str, SENSITIVE));
  411. }
  412.  
  413.  
  414. // operator< -- Test for lexical ordering before a CoolString
  415. // Input:       this* CoolString pointer, char* pointer
  416. // Output:      Boolean TRUE/FALSE
  417.  
  418. inline Boolean CoolString::operator< (const char* c) const {
  419.   return (is_lt (*this, c, SENSITIVE));
  420. }
  421.  
  422.  
  423. // operator> -- Test for lexical ordering after a CoolString
  424. // Input:       this* CoolString pointer, CoolString reference
  425. // Output:      Boolean TRUE/FALSE
  426.  
  427. inline Boolean CoolString::operator> (const CoolString& s) const {
  428.   return (is_gt (*this, s.str, SENSITIVE));
  429. }
  430.  
  431.  
  432. // operator> -- Test for lexical ordering after a CoolString
  433. // Input:       this* CoolString pointer, char* pointer
  434. // Output:      Boolean TRUE/FALSE
  435.  
  436. inline Boolean CoolString::operator> (const char* c) const {
  437.   return (is_gt (*this, c, SENSITIVE));
  438. }
  439.  
  440.  
  441. // operator<= -- Test for lexical ordering before or equal to a CoolString
  442. // Input:        this* CoolString pointer, CoolString reference
  443. // Output:       Boolean TRUE/FALSE
  444.  
  445. inline Boolean CoolString::operator<= (const CoolString& s) const {
  446.   return (is_le (*this, s.str, SENSITIVE));
  447. }
  448.  
  449.  
  450. // operator<= -- Test for lexical ordering before or equal to a CoolString
  451. // Input:        this* CoolString pointer, char* pointer
  452. // Output:       Boolean TRUE/FALSE
  453.  
  454. inline Boolean CoolString::operator<= (const char* c) const {
  455.   return (is_le (*this, c, SENSITIVE));
  456. }
  457.  
  458.  
  459. // operator>= -- Test for lexical ordering after or equal to a CoolString
  460. // Input:        this* CoolString pointer, CoolString reference
  461. // Output:       Boolean TRUE/FALSE
  462.  
  463. inline Boolean CoolString::operator>= (const CoolString& s) const {
  464.   return (is_ge (*this, s.str, SENSITIVE));
  465. }
  466.  
  467.  
  468. // operator>= -- Test for lexical ordering after or equal to a CoolString
  469. // Input:        this* CoolString pointer, char* pointer
  470. // Output:       Boolean TRUE/FALSE
  471.  
  472. inline Boolean CoolString::operator>= (const char* c) const {
  473.   return (is_ge (*this, c, SENSITIVE));
  474. }
  475.  
  476.  
  477. // strlen -- Return the number of characters in the CoolString
  478. // Input:    CoolString reference
  479. // Output:   Length of the string
  480.  
  481. inline long strlen (const CoolString& s) {
  482.   return (s.length);
  483. }
  484.  
  485.  
  486. // atof -- Equivalent to strtod (str, (char**)END_OF_STRING)
  487. // Input:  Reference to CoolString object
  488. // Output: Double representing value contained in CoolString
  489.  
  490. inline double atof (const CoolString& s) {
  491.   return (strtod ((char *)s.str, (char **) END_OF_STRING));
  492. }
  493.  
  494.  
  495. // strncpy -- Returns s, with the first length characters of source copied
  496. //            into it.  The old value of s is lost.
  497. // Input   -- A reference to a CoolString s, a char* source, and an int length.
  498. // Output  -- The modified CoolString s.
  499.  
  500. inline CoolString& strncpy(CoolString& s, const char* source, int n) {
  501.   return strncpy(s, source, (long) n);
  502. }
  503.  
  504. #endif                        // End #ifdef of STRINGH
  505.  
  506.  
  507.